const app = getApp();
const api = require("../../utils/api.js");
const request = require("../../service/request.js");

var page = 0;
var page1 = 0;
var page2 = 0;


Page( {  
  data: {  
    /** 
        * 页面配置 
        */  
    winWidth: 0,  
    winHeight: 0,  
    // tab切换  
    currentTab: 0,  
    // collectionStatus: [false,false],
    // collectionNum:[18,20],
    colorArr: ["#6797f7", "#f2d898", "#9ce5a9", "#ed7878","#ebabf2"],
    // 随机颜色数组
    randomColor:[],
    updateShow:"none"
  },  
  onLoad: function() {  
    var that = this;  

    //判断是否是刷新操作
    if(app.globalData.isUpdate){
      that.setData({
        updateShow: 'block'
      })
      setTimeout(function () {
        that.setData({
          updateShow: 'none'
        })
      }, 3000);
      app.globalData.isUpdate = false;
    }

    wx.showToast({
      title:"加载中...",
      icon:"loading",
      duration:12000
    });

//首页获取睿思校招和就业信息网数据(API在一个库中，写在一起)
    var API_RS = api.indexRuisi + "&page=" + page,
        API_JYXX = api.indexJyxx + "&page=" + page1,
        API_XJH = api.indexSeminar + "?page=" + page2;
    //登录获取sessionid
    app.userLogin().then(function(){
      //获取sessionid后调用睿思API
      request.get(API_RS,{},app.globalData.sessionid).then(res => {
        wx.hideToast();//获取数据成功，隐藏"加载中"的Toast
        console.log("sessionid:"+app.globalData.sessionid);
        console.log("调用睿思API成功");
        that.setData({
            list:res.data
        })
        that.randomColor();
        //调用就业信息网API
        request.get(API_JYXX,{},app.globalData.sessionid).then(res => {
          console.log("调用就业信息网API成功");
          var data = res.data;
          that.setData({
            list1:data
          });
          that.randomColor1();
          //调用宣讲会API
          request.get(API_XJH,{},app.globalData.sessionid).then(res => {
            console.log("宣讲会调用成功");
            console.log(res.data)
            that.setData({
              list2:res.data
            })
          })

        })
      })
    })
    
    /** 
     * 获取系统信息 
     */  
    wx.getSystemInfo( {  
  
      success: function( res ) {  
        that.setData( {  
          winWidth: res.windowWidth,  
          winHeight: res.windowHeight  
        });  
      }  
  
    });

    // 调用函数时，传入new Date()参数，返回值是日期和时间
    // 再通过setData更改Page()里面的data，动态更新页面的数据
    var timestamp = Date.parse(new Date());
    //返回当前时间毫秒数
    timestamp = timestamp / 1000;
    var n = timestamp *1000;
    var date = new Date(n);
    //月
    var M = date.getMonth() + 1;
    //var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //日
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();

    this.setData({
      M: M,
      D: D
    });

    
  },  

  //随机背景色函数:randomColor对应睿思校招，randomColor1对应就业信息
  randomColor:function(){
    var dataLen = this.data.list.length;
    // 必须经过第三方变量接收才能使用
    var len = this.data.colorArr.length;
    var arr = this.data.colorArr;
    var newArr = [];
    // 不能只执行一次，要执行多次(dataArr.length次)
    //要获取跟数据同等个数的随机颜色值
    while (dataLen>0){
      // 获取随机颜色
      var random = arr[Math.floor(Math.random() * len)];
      newArr.push(random);
      dataLen--;
    }
    // 将随机颜色数组赋值给randomColor
    this.setData({ randomColor: newArr });
  },
  randomColor1:function(){
    var dataLen = this.data.list1.length;
    // 必须经过第三方变量接收才能使用
    var len = this.data.colorArr.length;
    var arr = this.data.colorArr;
    var newArr = [];
    // 不能只执行一次，要执行多次(dataArr.length次)
    //要获取跟数据同等个数的随机颜色值
    while (dataLen>0){
      // 获取随机颜色
      var random = arr[Math.floor(Math.random() * len)];
      newArr.push(random);
      dataLen--;
    }
    // 将随机颜色数组赋值给randomColor
    this.setData({ randomColor1: newArr });
  },
  /** 
     * 滑动切换tab 
     */  
  bindChange: function( e ) {  
  
    var that = this;  
    that.setData( { currentTab: e.detail.current });  
  
  },  
  /** 
   * 点击tab切换 
   */  
  swichNav: function( e ) {  
  
    var that = this;  
  
    if( this.data.currentTab === e.target.dataset.current ) {  
      return false;  
    } else {  
      that.setData( {  
        currentTab: e.target.dataset.current  
      })  
    }  
  },

  //触底加载更多
  pullUpLoad: function() {
    var that = this; 
    if (that.data.currentTab===0) {
      page++;
      var API_RS = api.indexRuisi + "&page=" + page;
      request.get(API_RS, {}, app.globalData.sessionid).then(res => {
        var isBottom = res.data?true:false;

          
          if(isBottom){
            wx.showLoading({
              title: '加载更多数据',
              icon: 'loading',
              duration:2000
            })
            var moment_list = that.data.list;
            for (var i = 0; i < res.data.length; i++) {
              moment_list.push(res.data[i]);
            }

            that.setData({
              list:moment_list
            })

            that.randomColor();

            wx.hideLoading();
            console.log("睿思校招加载page="+page);
            console.log(res.data);
          } else {
            wx.showToast({
              title: '亲，到底啦！',
              icon: 'none',
              duration:1000
            })
            page--;
            console.log("睿思校招加载page="+page);
          }
      })
    } else if(that.data.currentTab===1) {
        page1++;
        var API_JYXX = api.indexJyxx + "&page=" + page1;
        request.get(API_JYXX, {}, app.globalData.sessionid).then(res => {
          var isBottom = res.data?true:false;
            if(isBottom){
              wx.showLoading({
                title: '加载更多数据',
                icon: 'loading',
                duration:2000
              })
              var moment_list = that.data.list1;
              for (var i = 0; i < res.data.length; i++) {
                moment_list.push(res.data[i]);
              }

              that.setData({
                list1:moment_list
              })
              that.randomColor1();
              wx.hideLoading();
              console.log("就业信息网加载page="+page1);
              console.log(res.data);
            } else {
              wx.showToast({
                title: '亲，到底啦！',
                icon: 'none',
                duration:1000
              })
              page1--;
              console.log("就业信息网加载page="+page1);
            }
        })  
      } else if(that.data.currentTab===2) {
          page2++;
          var API_XJH = api.indexSeminar + "?page=" + page2;
          request.get(API_XJH, {}, app.globalData.sessionid).then(res => {
            var isBottom = res.data?true:false;
              if(isBottom){
                wx.showLoading({
                  title: '加载更多数据',
                  icon: 'loading',
                  duration:2000
                })
                var moment_list = that.data.list2;
                for (var i = 0; i < res.data.length; i++) {
                  moment_list.push(res.data[i]);
                }
                that.setData({
                  list2:moment_list
                })
                wx.hideLoading();
                console.log("宣讲会加载page="+page2);
                console.log(res.data);
              } else {
                wx.showToast({
                  title: '亲，到底啦！',
                  icon: 'none',
                  duration:1000
                })
                page2--;
                console.log("宣讲会加载page="+page2);
              }
          })   
      }   
    },
    onPullDownRefresh: function () {
      wx.stopPullDownRefresh();
      let that = this;
      page = 0;
      page1 = 0;
      page2 = 0;
      console.log( "下拉刷新...." )
      wx.reLaunch({
        url:"/pages/index/index",
        success:function(){
          console.log(1);
        }
      })
      app.globalData.isUpdate = true;
    },
    //分享
    onShareAppMessage: function () {
      let that = this;
      return {
        title: '校招SSP', // 转发后 所显示的title
        path: '/pages/index/index', // 相对的路径
        success: (res)=>{    // 成功后要做的事情
          console.log(res)
          console.log("1"+res.shareTickets[0])
          wx.showToast({
            title:"分享成功^O^",
            icon:"success",
            duration:2500
          });
          wx.getShareInfo({
            shareTicket: res.shareTickets[0],
            success: (res)=> { 
              // that.setData({
              //   isShow:true
              // }) 
              // console.log(that.setData.isShow)
              console.log("2"+res.data)
             },
            fail: function (res) { console.log("失败"+res) },
            complete: function (res) { console.log("成功"+res) }
          })
        },
        fail: function (res) {
          // 分享失败
          console.log(res)
        }
      }
    }

  // onCollection: function(){
  //   var that = this
  //   var collectionStatus = that.data.collectionStatus[0]
  //   let collectionNum = that.data.collectionNum[0]
  //   this.setData({
  //     "collectionStatus[0]": !this.data.collectionStatus[0],
      
  //   })
  //   if(!collectionStatus){
  //     //如果当前状态是未收藏
  //     collectionNum++
  //     //console.log(collectionNum)
  //     that.setData({
  //       "collectionNum[0]":collectionNum
  //     })
  //     //console.log(this.data.collectionNum)
  //     collectionStatus = true;
  //   }else{
  //     //如果当前状态是已收藏
  //     collectionNum--;
  //     that.setData({
  //       "collectionNum[0]":collectionNum
  //     })
  //     collectionStatus = false;
  //   }
  // },

  // onCollection1: function(){
  //   var that = this
  //   var collectionStatus = that.data.collectionStatus[1]
  //   let collectionNum = that.data.collectionNum[1]
  //   this.setData({
  //     "collectionStatus[1]": !this.data.collectionStatus[1]
  //   })
  //   if(!collectionStatus){
  //     //如果当前状态是未收藏
  //     collectionNum++
  //     //console.log(collectionNum)
  //     that.setData({
  //       "collectionNum[1]":collectionNum
  //     })
  //     //console.log(this.data.collectionNum)
  //     collectionStatus = true;
  //   }else{
  //     //如果当前状态是已收藏
  //     collectionNum--;
  //     that.setData({
  //       "collectionNum[1]":collectionNum
  //     })
  //     collectionStatus = false;
  //   }
  // }
})  










